{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-21T09:19:13.937531900Z",
     "start_time": "2024-02-21T09:19:13.267899500Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "from advisor_backend.interface import Interface\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 算子调优分析\n",
    "## 1. 算子分析的数据准备\n",
    "当前算子分析工具支持分析Ascend Pyorch Profiler方式生成的ascend_pt目录\n",
    "## 2. 算子分析解决的问题\n",
    "当前支持分析模型中存在可融合的小算子，并给出优化建议。\n",
    "\n",
    "\"更多融合算子信息，请查阅 https://www.hiascend.com/document/detail/zh/CANNCommunityEdition/700alpha003/processormodel/hardwaredesc_0001.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-22T08:41:17.455567500Z",
     "start_time": "2024-02-22T08:41:16.716884800Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[INFO] Start to analyse the target file: C:\\data\\ascend_pt\\ASCEND_PROFILER_OUTPUT\\kernel_details.csv\n",
      "            pattern_name                     pattern  len  count  duration sum(us)          op durations(us) index\n",
      "18  torch_npu.npu_swiglu  (Slice, Slice, Swish, Mul)    4      1             12.56  [3.14, 3.14, 3.14, 3.14]   [0]\n",
      "\n",
      "\n",
      "The computing time of fusable op is 12.56 ms.\n",
      "\n",
      "\n",
      "Advice 0:\n",
      "Replace [Slice, Slice, Swish, Mul] with torch_npu.npu_swiglu. This pattern first happened in: \n",
      "torch/nn/modules/module.py(1513): _call_impl\n",
      "profiler_main.py(116):forward\n"
     ]
    }
   ],
   "source": [
    "# EDIT THE PROFILING DATA PATH\n",
    "compute_path = \"[YOUR PATH]\"\n",
    "interface = Interface(compute_path)\n",
    "data = interface.get_data('compute', 'npu_fused')\n",
    "pd.set_option('display.max_columns', None)\n",
    "pd.set_option('display.width', 900)\n",
    "print(data['data'].iloc[:, :-2])\n",
    "print('\\n')\n",
    "print(data['bottleneck'])\n",
    "print('\\n')\n",
    "print(data['advice'])"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3 (ipykernel)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
